<template>
  <div id="lib-login-box">
    <div id="lib-login-header" :class="[windowIsFocus === 1 ? 'window-focus' : 'window-blur']">

    </div>
    <div id="lib-login-main">
      <h1 style="margin-bottom: 150px;">欢迎使用LIB系统</h1>
      <el-form label-position="top" size="small" :model="loginForm" style="margin-bottom: 100px;">
        <el-form-item label="用户名">
          <el-input placeholder="请输入账号" style="width:200px;" v-model="loginForm.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input placeholder="请输入密码" style="width:200px;" v-model="loginForm.password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: auto;" @click="submitLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
export default {
  inject:['accountPassword'],
  beforeCreate() {
    window.electronAPI.windowBlur((_event, value) => {
      if (value === 0) this.windowIsFocus = 0
    })
    window.electronAPI.windowFocus((_event, value) => {
      if (value === 1) this.windowIsFocus = 1
    })
  },
  methods: {
    submitLogin(){
      if(
        this.loginForm.username === this.accountPassword.username && 
        this.loginForm.password === this.accountPassword.password
      ){
        window.sessionStorage.setItem('is_authorized', 'authorized')
        ElMessage({
          message: '登录成功!',
          type: 'success',
        })
        this.$router.replace({path: '/'})
      }else {
        ElMessage.error('密码或账号错误!!!')
      }
    }
  },
  data() {
    return {
      windowIsFocus: 1,
      loginForm: {
        username: '',
        password: ''
      }
    }
  }
}
</script>

<style>
#lib-login-box {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#lib-login-header {
  -webkit-app-region: drag;
  height: 34px;
  width: 100%;
}
#lib-login-main {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  height: 100%;
  background-color: rgb(37, 37, 38);
}
</style>